//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "SobolBrownianGeneratorFactory.h"
using namespace Cephei::QL::Models::Marketmodels::Browniangenerators;
#include <gen/QL/Models/Marketmodels/BrownianGenerator.h>
#include <gen/QL/Models/Marketmodels/BrownianGeneratorFactory.h>
using namespace Cephei::QL::Models::Marketmodels;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGeneratorFactory::CSobolBrownianGeneratorFactory (QL::Models::Marketmodels::Browniangenerators::SobolBrownianGenerator::OrderingEnum ordering, Microsoft::FSharp::Core::FSharpOption<QL::Math::Randomnumbers::SobolRsg::DirectionIntegersEnum>^ directionIntegers) : CBrownianGeneratorFactory(CSobolBrownianGeneratorFactory::typeid)
{
    try
    {
#ifdef HANDLE
        _phSobolBrownianGeneratorFactory = NULL;
#endif
        QuantLib::SobolBrownianGenerator::Ordering _ordering = (QuantLib::SobolBrownianGenerator::Ordering)ordering ;
        QuantLib::SobolRsg::DirectionIntegers _directionIntegers = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Math::Randomnumbers::SobolRsg::DirectionIntegersEnum>::IsSome::get (directionIntegers) ? (QuantLib::SobolRsg::DirectionIntegers)directionIntegers->Value : QuantLib::SobolRsg::DirectionIntegers::Jaeckel); //10
        _ppSobolBrownianGeneratorFactory = new boost::shared_ptr<QuantLib::SobolBrownianGeneratorFactory> (new QuantLib::SobolBrownianGeneratorFactory ( _ordering,  _directionIntegers ));
        SetBrownianGeneratorFactory (boost::dynamic_pointer_cast<QuantLib::BrownianGeneratorFactory> (*_ppSobolBrownianGeneratorFactory));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGeneratorFactory::CSobolBrownianGeneratorFactory (boost::shared_ptr<QuantLib::SobolBrownianGeneratorFactory>& childNative, Object^ owner) : CBrownianGeneratorFactory(CSobolBrownianGeneratorFactory::typeid)
{
#ifdef HANDLE
	_phSobolBrownianGeneratorFactory = NULL;
#endif
	_ppSobolBrownianGeneratorFactory = &childNative;
    _ppBrownianGeneratorFactory = new boost::shared_ptr<QuantLib::BrownianGeneratorFactory> (boost::dynamic_pointer_cast<QuantLib::BrownianGeneratorFactory> (*_ppSobolBrownianGeneratorFactory));
}
Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGeneratorFactory::CSobolBrownianGeneratorFactory (QuantLib::SobolBrownianGeneratorFactory& childNative, Object^ owner) : CBrownianGeneratorFactory(CSobolBrownianGeneratorFactory::typeid)
{
#ifdef HANDLE
	_phSobolBrownianGeneratorFactory = NULL;
#endif
	_ppSobolBrownianGeneratorFactory = new boost::shared_ptr<QuantLib::SobolBrownianGeneratorFactory> (&childNative);
    _ppBrownianGeneratorFactory = new boost::shared_ptr<QuantLib::BrownianGeneratorFactory> (boost::dynamic_pointer_cast<QuantLib::BrownianGeneratorFactory> (*_ppSobolBrownianGeneratorFactory));
    _SobolBrownianGeneratorFactoryOwner = owner;
    _BrownianGeneratorFactoryOwner = owner;
}

Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGeneratorFactory::CSobolBrownianGeneratorFactory (CSobolBrownianGeneratorFactory^ copy) : CBrownianGeneratorFactory(CSobolBrownianGeneratorFactory::typeid)
{
#ifdef HANDLE
	_phSobolBrownianGeneratorFactory = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSobolBrownianGeneratorFactory = new boost::shared_ptr<QuantLib::SobolBrownianGeneratorFactory> (copy->GetShared());
        _ppBrownianGeneratorFactory = new boost::shared_ptr<QuantLib::BrownianGeneratorFactory> (boost::dynamic_pointer_cast<QuantLib::BrownianGeneratorFactory> (*_ppSobolBrownianGeneratorFactory));
    }
}
Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGeneratorFactory::CSobolBrownianGeneratorFactory (PLATFORM::Type^ t) : CBrownianGeneratorFactory(CSobolBrownianGeneratorFactory::typeid)
{
#ifdef HANDLE
	_phSobolBrownianGeneratorFactory = NULL;
#endif
	if (!t->IsSubclassOf(CSobolBrownianGeneratorFactory::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGeneratorFactory::CSobolBrownianGeneratorFactory (QuantLib::Handle<QuantLib::SobolBrownianGeneratorFactory>& childNative, Object^ owner)  : CBrownianGeneratorFactory(CSobolBrownianGeneratorFactory::typeid)
{
	_phSobolBrownianGeneratorFactory = &childNative;
	_ppSobolBrownianGeneratorFactory = &static_cast<boost::shared_ptr<QuantLib::SobolBrownianGeneratorFactory>>(childNative.currentLink());
    _ppBrownianGeneratorFactory = new boost::shared_ptr<QuantLib::BrownianGeneratorFactory> (boost::dynamic_pointer_cast<QuantLib::BrownianGeneratorFactory> (*_ppSobolBrownianGeneratorFactory));
    _SobolBrownianGeneratorFactoryOwner = owner;
}
Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGeneratorFactory::CSobolBrownianGeneratorFactory (QuantLib::Handle<QuantLib::SobolBrownianGeneratorFactory> childNative)  : CBrownianGeneratorFactory(CSobolBrownianGeneratorFactory::typeid)
{
	_phSobolBrownianGeneratorFactory = &childNative;
	_ppSobolBrownianGeneratorFactory = &static_cast<boost::shared_ptr<QuantLib::SobolBrownianGeneratorFactory>>(childNative.currentLink());
    _ppBrownianGeneratorFactory = new boost::shared_ptr<QuantLib::BrownianGeneratorFactory> (boost::dynamic_pointer_cast<QuantLib::BrownianGeneratorFactory> (*_ppSobolBrownianGeneratorFactory));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGeneratorFactory::CSobolBrownianGeneratorFactory (QuantLib::SobolBrownianGeneratorFactory childNative)  : CBrownianGeneratorFactory(CSobolBrownianGeneratorFactory::typeid)
{
#ifdef HANDLE
	_phSobolBrownianGeneratorFactory = NULL;
#endif
	_ppSobolBrownianGeneratorFactory = new boost::shared_ptr<QuantLib::SobolBrownianGeneratorFactory> (new QuantLib::SobolBrownianGeneratorFactory (childNative));
    _ppBrownianGeneratorFactory = new boost::shared_ptr<QuantLib::BrownianGeneratorFactory> (boost::dynamic_pointer_cast<QuantLib::BrownianGeneratorFactory> (*_ppSobolBrownianGeneratorFactory));
}
#endif

Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGeneratorFactory::~CSobolBrownianGeneratorFactory ()
{
    if (_ppSobolBrownianGeneratorFactory != NULL)
    {
	    delete _ppSobolBrownianGeneratorFactory;
        _ppSobolBrownianGeneratorFactory = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGeneratorFactory::!CSobolBrownianGeneratorFactory ()
{
    if (_ppSobolBrownianGeneratorFactory != NULL)
    {
	    delete _ppSobolBrownianGeneratorFactory;
    }
}
QuantLib::SobolBrownianGeneratorFactory& Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGeneratorFactory::GetReference ()
{
    if (_ppSobolBrownianGeneratorFactory == NULL) throw REFNEW NativeNullException ();
	return **_ppSobolBrownianGeneratorFactory;
}
boost::shared_ptr<QuantLib::SobolBrownianGeneratorFactory>& Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGeneratorFactory::GetShared ()
{
    if (_ppSobolBrownianGeneratorFactory == NULL) throw REFNEW NativeNullException ();
	return *_ppSobolBrownianGeneratorFactory;
}
QuantLib::SobolBrownianGeneratorFactory* Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGeneratorFactory::GetPointer ()
{
    if (_ppSobolBrownianGeneratorFactory == NULL) throw REFNEW NativeNullException ();
	return &**_ppSobolBrownianGeneratorFactory;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SobolBrownianGeneratorFactory>& Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGeneratorFactory::GetHandle ()
{
	if (_phSobolBrownianGeneratorFactory == NULL)
	{
		_phSobolBrownianGeneratorFactory = new Handle<QuantLib::SobolBrownianGeneratorFactory> (*_ppSobolBrownianGeneratorFactory);
	}
	return *_phSobolBrownianGeneratorFactory;
}
#endif
bool Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGeneratorFactory::HasNative () 
{
	return (_ppSobolBrownianGeneratorFactory != NULL);
}

Cephei::QL::Models::Marketmodels::IBrownianGenerator^ Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGeneratorFactory::Create (UInt64 factors, UInt64 steps)
{
    try
    {
        QuantLib::Size _factors = (QuantLib::Size)ValueHelper::Convert (factors); //a
        QuantLib::Size _steps = (QuantLib::Size)ValueHelper::Convert (steps); //a
    	boost::shared_ptr<QuantLib::BrownianGenerator> _rv = (boost::shared_ptr<QuantLib::BrownianGenerator>)(*_ppSobolBrownianGeneratorFactory)->create ( _factors,  _steps );   
        Cephei::QL::Models::Marketmodels::CBrownianGenerator^ _nrv = REFNEW Cephei::QL::Models::Marketmodels::CBrownianGenerator (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Browniangenerators::ISobolBrownianGeneratorFactory^ Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGeneratorFactory_Factory::Create (QL::Models::Marketmodels::Browniangenerators::SobolBrownianGenerator::OrderingEnum ordering, Microsoft::FSharp::Core::FSharpOption<QL::Math::Randomnumbers::SobolRsg::DirectionIntegersEnum>^ directionIntegers)
{
    return REFNEW CSobolBrownianGeneratorFactory ( ordering,  directionIntegers);
}
